//
//  EquipmentNameVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/14.
//  Copyright © 2020 tym. All rights reserved.
//

#import "EquipmentNameVC.h"
#import "EquipmentService.h"
#import "UIimage+Extends.h"
#import "UIButton+Gradient.h"
@interface EquipmentNameVC ()

@property(nonatomic,weak) IBOutlet UITextField *equipName;

@property(nonatomic,weak) IBOutlet UIButton *subBtn;

@property(nonatomic,strong) EquipmentService *services;
@end

@implementation EquipmentNameVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"设备名称"];
    
    self.subBtn.layer.masksToBounds = YES;
    self.subBtn.layer.cornerRadius = 5;
    
    [self.equipName addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self changedTextField:nil];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.equipName.text = self.nameStr;
}

-(void)changedTextField:(UITextField*)fid{
    if (self.equipName.text.length<1) {
        [self.subBtn setBackgroundImage:[UIImage imageWithColor:rgba(230, 230, 230) withSize:self.subBtn.bounds.size] forState:UIControlStateNormal];
    }else{
        [self.subBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
    }
}

-(EquipmentService*)services{
    if (_services == nil) {
        _services = [[EquipmentService alloc] init];
    }
    return _services;
}

-(IBAction)doSubmitAction:(id)sender{
    
    TYMWeak(self);
    [self.services getUpdateequipment:self.equipMentID name:self.equipName.text completion:^(NSError * _Nonnull error, LMJBaseResponse * _Nonnull response) {
        if(response.message){
            [WHToast showMessage:response.message duration:2 finishHandler:nil];
        }else{
            [WHToast showMessage:@"修改成功" duration:2 finishHandler:^{
                
                if (weakself.equipmentNameBlock) {
                    weakself.equipmentNameBlock(self.equipName.text);
                }
                [weakself.navigationController popViewControllerAnimated:YES];
                
            }];
        }
    }];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSString *blank = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""];
    if(![string isEqualToString:blank]) {
        return NO;
    }
    if (textField.text.length >= 8 && ![string isEqualToString:@""]) {
        return NO;
    }
    return YES;
}
@end
